import pygame
from config import GameConfig
import ScoresRank


class GameUI:
    @staticmethod
    def button(surface, msg, x, y, w, h, ic, ac, action=None, *args):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        # 设置鼠标悬停高亮和点击事件
        if x + w > mouse[0] > x and y + h > mouse[1] > y:
            pygame.draw.rect(surface, ac, ((x, y), (w, h)))
            if click[0] == 1 and action is not None:
                action(*args)
        else:
            pygame.draw.rect(surface, ic, ((x, y), (w, h)))
        # 设置按钮中的文字样式和居中对齐
        font = pygame.font.SysFont('simHei', 20)
        smallfont = font.render(msg, True, GameConfig.white)
        smallrect = smallfont.get_rect()
        smallrect.center = ((x + (w / 2)), (y + (h / 2)))
        surface.blit(smallfont, smallrect)

    @classmethod
    def main_menu(cls, surface, start, user, scoreRank):
        init_background = pygame.image.load(GameConfig.init_bg)
        surface.blit(init_background, (0, 0))
        # 设置字体
        font = pygame.font.SysFont("simHei", 50)
        # 初始界面显示文字
        fontsurf = font.render('欢迎来到贪吃蛇大冒险!', True, GameConfig.black)  # 文字
        fontrect = fontsurf.get_rect()
        fontrect.center = ((GameConfig.win_width / 2), 200)
        surface.blit(fontsurf, fontrect)
        GameUI.button(
            surface,
            "登录/注册",
            370,
            250,
            200,
            40,
            GameConfig.green,
            GameConfig.brightred,
            user)  # 用户登录注册模块，若用户尚未注册则自动注册，若已注册则直接登录
        GameUI.button(
            surface,
            "单人闯关",
            370,
            300,
            200,
            40,
            GameConfig.blue,
            GameConfig.brightred,
            start)
        GameUI.button(
            surface,
            "继续游戏",
            370,
            350,
            200,
            40,
            GameConfig.blue,
            GameConfig.brightred)
        GameUI.button(
            surface,
            "排行榜",
            370,
            400,
            200,
            40,
            GameConfig.blue,
            GameConfig.brightred,
            scoreRank)
        GameUI.button(
            surface,
            "退出",
            370,
            450,
            200,
            40,
            GameConfig.red,
            GameConfig.brightred,
            quit)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
        pygame.display.update()

    @classmethod
    def pause_menu(cls, surface, game_continue):
        init_background = pygame.image.load(GameConfig.init_bg)
        surface.blit(init_background, (0, 0))
        font = pygame.font.SysFont("simHei", 40)
        fontsurf = font.render('游戏暂停', True, GameConfig.black)  # 文字
        fontrect = fontsurf.get_rect()
        fontrect.center = ((GameConfig.win_width / 2), 100)
        surface.blit(fontsurf, fontrect)
        GameUI.button(
            surface,
            "保存游戏",
            380,
            200,
            200,
            40,
            GameConfig.blue,
            GameConfig.brightred)
        GameUI.button(
            surface,
            "继续游戏",
            380,
            250,
            200,
            40,
            GameConfig.blue,
            GameConfig.brightred,
            game_continue)
        GameUI.button(
            surface,
            "结束游戏",
            380,
            300,
            200,
            40,
            GameConfig.blue,
            GameConfig.brightred,
            quit)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
        pygame.display.update()

    @classmethod
    def stop_menu(cls, surface, restart, score):
        init_background = pygame.image.load(GameConfig.init_bg)
        surface.blit(init_background, (0, 0))
        font = pygame.font.SysFont("simHei", 40)
        fontsurf = font.render(
            '游戏结束! 你的得分为: %s' %
            score, False, GameConfig.black)
        surface.blit(fontsurf, (250, 200))
        GameUI.button(
            surface,
            "重新游戏",
            370,
            320,
            200,
            40,
            GameConfig.blue,
            GameConfig.brightred,
            restart)
        GameUI.button(
            surface,
            "结束游戏",
            370,
            370,
            200,
            40,
            GameConfig.blue,
            GameConfig.brightred,
            quit)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
        pygame.display.update()

    @classmethod
    def inform_menu(cls, surface, score, level):
        font = pygame.font.SysFont("simHei", 25)
        mode_title = font.render('单人闯关', False, GameConfig.grey)
        socre_title = font.render('得分: %s' % score, False, GameConfig.grey)
        level_title = font.render('第 %s 关' % level, False, GameConfig.grey)
        surface.blit(mode_title, (50, 30))
        surface.blit(level_title, (50, 65))
        surface.blit(socre_title, (50, 100))
        pygame.display.update()
